package libs;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import bookException.BookExceptionCode;


/**
 *
 * @author Alex
 */
public class DataBase {

	private String login = "root";
    private String pass = "1";
    private String url = "jdbc:mysql://localhost/telephonebook";
    private Connection con = null;
    private Statement stmt = null;
    private static volatile DataBase instance;
    
    private DataBase() {}
    
    public static DataBase getInstance() {
		if(instance == null)
		{
			synchronized (DataBase.class){
				if(instance == null)
					instance = new DataBase();
			}
		}
		return instance;
	}
    
    public DataBase(String l, String p , String u) {
        this.login = l;
        this.pass = p;
        this.url = u;
    }
    
    
    public void Connect(){
        try {
        	Class.forName("com.mysql.jdbc.Driver").newInstance();
            this.con = DriverManager.getConnection(this.url,this.login,this.pass);
            this.stmt = (Statement) con.createStatement();
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
    
    public void DisConnect(){
        try {
            if (con != null) {
                con.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
    
    protected void Execute(String s){
         try {
             this.stmt.execute(s);
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
    
    public ResultSet Select(String [] fields, String [] tables, String condition,String additional){
        String sql = "SELECT ";
        for (int i = 0; i < fields.length; i++) {
            sql += i != fields.length - 1 ? fields[i]+"," : fields[i];
        }
        sql += " FROM ";
        for (int i = 0; i < tables.length; i++) {
            sql += i != tables.length - 1 ? tables[i]+"," : tables[i];
        }
        if (!condition.isEmpty()) {
            sql += " WHERE "+condition;
        }
        sql += " "+additional;
        //System.out.println(sql);
        try {
             return this.stmt.executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
            
        }
        return null;
    }
    
    public void Insert(String table, String [] fields, String [] values){
        String f = "";
        String v = "";
        for (int i = 0; i < fields.length; i++) {
            f += i != fields.length - 1 ? fields[i]+"," : fields[i];
        }
        for (int i = 0; i < values.length; i++) {
            v += i != values.length - 1 ? "'"+values[i]+"'," : "'"+values[i]+"'";
        }
        String sql = "INSERT INTO "+table+"("+f+") VALUES ("+v+")";
        //System.out.println(sql);
        this.Execute(sql);
    }
    
    public void Update(String table, String [] fields, String [] values, String condition){
        String sql = "UPDATE "+table+" set ";
        for (int i = 0; i < fields.length; i++) {
            //f += i != fields.length - 1 ? fields[i]+"," : fields[i];
            sql += fields[i]+" = '"+values[i]+"'";
            sql += i != fields.length - 1 ? ", " : "";
        }
        
        if (!condition.isEmpty()) {
            sql += " WHERE "+condition;
        }
        //System.out.println(sql);
        this.Execute(sql);
    }
 
    public void Replace(String table, String [] fields, String [] values, String condition){
        
        String sql = "REPLACE INTO "+table+" set ";
        for (int i = 0; i < fields.length; i++) {
            sql +=fields[0]+" = '"+values[0]+"',";
        }
        sql = sql.substring(0,sql.length()-1);
        if (!condition.isEmpty()) {
            sql += " WHERE "+condition;
        }
        //System.out.println(sql);
        this.Execute(sql);
    }
    
    public void Delete(String table, String condition){
        String sql = "DELETE FROM "+table;
        if (!condition.isEmpty()) {
            sql += " WHERE "+condition;
        }
        //System.out.println(sql);
        this.Execute(sql);
    }
}
